#include "mvc/model/project.h"
#include "common/settings.h"
#include "common/log/QsLog.h"
#include "common/xmlparser.h"

#include <iostream>

#include <QFile>

/**
  * The Project constructor
  * dir -  the directory of the current project
  * file_name - the filename of the current project
  * st - the state of the current project, if it's a NEW project or an EXISTING project
  * n_configs - the total configurations of the current project
  */
Project::Project(const QString &dir, const QString &file_name, int st, int n_configs)
    : _dir(dir), _filename(file_name)
{
    _state = st;
    _nconfigs = n_configs;

    QLOG_INFO() << "Created project: " << file_name << " from directory " << dir << " with state " << st <<
                   " and " << n_configs << " configurations.";
}

/**
  * returns the full path of the project
  */
const QString Project::full_path() const
{
//    cout << "TODO: Project.full_path" << endl;
    QString _full_path = projects_directory();

    _full_path.append("/").append(_dir);
    _full_path.append("/").append(_filename);

    return _full_path;
}

/**
  * change the state of the project. NEW -> EXISTING, EXISTING -> NEW
  */
void Project::change_state()
{
    _state = (_state == NEW_PROJECT)? EXISTING_PROJECT : NEW_PROJECT;
}


/**
  * look for a file in a specified configuration directory the current project
  * configuration - the number of configuration
  * name_file - the filename to find
  * returns true if the filename is created in the configuration directory
  */
bool Project::file_exist(int configuration, const QString &name_file) const
{
    QString path = get_config_dir(configuration);
    path.append(name_file);

    QFile file(path);
    if (!file.open(QIODevice::ReadOnly))
    {
        std::cerr << "Cannot open the file: " << name_file.toStdString() << "::invalide file): " << path.toStdString() <<std::endl;
        file.close();
        return false;
    }

    file.close();
    return true;
}

/**
  * make the full name of a configuration directory
  * configuration - the configuration number
  */
QString Project::get_config_dir(int configuration) const{
    QString result_path;
    QString cnumber("");
    if(configuration < 10)
        cnumber.append("00");
    else if(configuration> 9 && configuration< 100)
        cnumber.append("0");

    result_path.append(projects_directory());
    result_path.append(_dir);
    result_path.append("/");
    result_path.append(QString(PREFIX).append(cnumber).append(QString::number(configuration)));
    result_path.append("/");
    return result_path;
}

/**
  * reads the parameters file
  * returns a vector of the all parameters in the file
  */
const QVector<Parameters *> Project::read_params_file(const QString &file) const
{
    QVector<Parameters *> valores(0);

    for (int i = 1 ; i < _nconfigs + 1; ++i)
    {
        QString path = get_config_dir(i);

        Parameters *pu = XmlParser::getInstance().readXml(file, path);

        valores.append(pu);
    }

    return valores;
}
